import settingFile from '@/setting.js'
import qs from './qs.js';
import homeApii from '@/api/homee'
import store from '@/store'
	import {
		mapMutations
	} from "vuex";
function http(options) {
	return new Promise((resolve, reject) => {
		// uni.showLoading({title: '加载中',mask:true});
		var setting = {
			url: settingFile.host1 + options.url,
			data: options.data,
			method: options.method.toUpperCase() || 'GET',
			timeout: 10000,
			header: {
				// 'content-type': 'application/x-www-form-urlencoded', // from data
				//'content-type': 'multipart/form-data', // from data
				//'content-type': 'application/json;charset:utf-8',
				'content-type': 'application/x-www-form-urlencoded',
				'channelId': 'FTSK20231214vaKTEoG22NGdCIXCrcFu'
			},
			success(res) {
			if(res.data.code==4011){
				uni.removeStorageSync("token") 
				let that = this
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log(res)
						let code = res.code
						// this.getopenid()
						const authCode = res.authCode;
						homeApii.getopenid({
							code: code
						}).then(res => {
							//console.log(res)
							if (res.code == 4000) {
								uni.setStorageSync("token", res.data.token)
								// that.$store.state.doc.token = res.data.token
								var pages = getCurrentPages();  // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面
								    var curPage = pages[pages.length - 1]; // 当前页面是这个页面栈数组中的最后一个
								let option=uni.getStorageSync("option")
								curPage.onLoad(option);//由于当前页面加载的时候，需要有个参数，这个参数可以在当前页面加载的时候保存一下。
							}
						})
					},
				})
				}
				resolve(res.data);
			},
			fail(err) {
				// setTimeout(function () {uni.hideLoading();}, 100);
				console.log('err', err);
				uni.showToast({
					icon: 'error',
					title: '网络超时!'
				})
				reject(err);
			},

		}
		if (options.header) {
			setting.header = Object.assign(setting.header, options.header);
		}
		uni.request(setting);
	})
}


export default http;